package cn.krui.project.controller.system;

import java.text.ParseException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import cn.krui.helper.bean.AppResult;
import cn.krui.helper.bean.DataArray;
import cn.krui.helper.db.DBHelper;
import cn.krui.helper.db.SQL;
import cn.krui.helper.tools.DateTimeUtils;
import cn.krui.project.bean.system.sys_params.sys_params;
import cn.krui.project.service.system.SysParamsService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam; 

/**
 * @program: project-system
 * @description: 系统配置模块
 * @author: zc
 * @create: 2019-10-10 16:05
 **/
@Api(tags = "管理-配置", description = "系统配置模块接口")
@RestController
@RequestMapping("/system/sysparams")
@CrossOrigin
public class SysParamsController extends SystemBaseController {

    @Autowired
    private SysParamsService sysParamsService;
    
	@Autowired
	private DBHelper dbHelper;

    /**
     * 系统配置列表
     */
    @PostMapping(value = "/getSysParamsList")
    @ApiOperation(value = "获取系统配置列表", httpMethod = "POST")
    public AppResult<sys_params> getSysParamsList(
    	  @ApiParam(value = "父级id",required=false) @RequestParam(value="pid",required=false)String pid,
          @ApiParam(value = "分类名称",required=false) @RequestParam(value="class_name",required=false)String class_name,
          @ApiParam(value = "分类标识",required=false) @RequestParam(value="name",required=false)String name,
          @ApiParam(value = "数值类型",required=false) @RequestParam(value="number_value",required=false)String number_value,
          @ApiParam(value = "日期类型",required=false) @RequestParam(value="date_value",required=false)String date_value,
          @ApiParam(value = "时间类型",required=false) @RequestParam(value="time_value",required=false)String time_value,
          @ApiParam(value = "字符类型",required=false) @RequestParam(value="string_value",required=false)String string_value,
          @ApiParam(value = "备注",required=false) @RequestParam(value="remark",required=false)String remark
    ) throws ParseException {
        AppResult<sys_params> app = new AppResult<sys_params>();
        sys_params sysParams=new sys_params();
        if(!StringUtils.isEmpty(pid)){
            sysParams.setPid(Integer.parseInt(pid));
        }
        if(!StringUtils.isEmpty(class_name)){
            sysParams.setClass_name(class_name);
        }
        if(!StringUtils.isEmpty(name)){
            sysParams.setName(name);
        }
        if(!StringUtils.isEmpty(number_value)){
            sysParams.setNumber_value(Double.valueOf(number_value));
        }
        if(!StringUtils.isEmpty(date_value)){ 
            sysParams.setDate_value(new DateTimeUtils(date_value).toDate()); 
        }
        if(!StringUtils.isEmpty(time_value)){ 
            sysParams.setTime_value(new DateTimeUtils(time_value).toDate());
        }
        if(!StringUtils.isEmpty(string_value)){
            sysParams.setString_value(string_value);
        }
        if(!StringUtils.isEmpty(remark)){
            sysParams.setRemark(remark);
        }
        List<sys_params> sysParamsInfos =  sysParamsService.selectSysPaList(sysParams);
        List<sys_params> childList = new ArrayList<>();
        for (sys_params nav : sysParamsInfos) {
        	/* 获取根节点下的所有子节点 使用getChild方法*/
        	nav.setChildren(this.getChild(nav.getId(),nav.getClass_name()));
        	childList.add(nav);
        }
	    app.ok();
	    DataArray<sys_params> list=new DataArray<sys_params>();
	    list.setDataList(childList);
	    list.setDataCount(childList.size());
	    app.setDataArray(list);
	    app.setMsg("获取成功");
        return app;
    }


    /**
     * 获取子节点
     * @param id 父节点id
     * @param allMenu 所有菜单列表
     * @return 每个根节点下，所有子部门列表
     */
    public List<sys_params> getChild(Integer id,String name){
        List<sys_params> list = new ArrayList<sys_params>();
		// 子菜单
		SQL.SelectSQL sql = SQL.select("*").from("qr_sys_params")
				.where("init_default=0 and pid=#{id}").addParameter("id", id);
		List<sys_params> childList = dbHelper.select(sql, sys_params.class);
		for (sys_params nav : childList) {
			nav.setP_name(name);
			nav.setChildren(this.getChild(nav.getId(), nav.getClass_name()));
			list.add(nav);
		}
        return list;
    }


    /**
     * 添加用户
     */
    @PostMapping(value = "/addSysParams")
    @ApiOperation(value = "添加系统配置", httpMethod = "POST")
    public AppResult<sys_params> addSysParams(
            @ApiParam(value = "父级id",required=true) @RequestParam(value="pid",required=true)String pid,
            @ApiParam(value = "分类名称",required=true) @RequestParam(value="class_name",required=true)String class_name,
            @ApiParam(value = "分类标识",required=false) @RequestParam(value="name",required=false)String name,
            @ApiParam(value = "数值类型",required=false) @RequestParam(value="number_value",required=false)String number_value,
            @ApiParam(value = "日期类型",required=false) @RequestParam(value="date_value",required=false)String date_value,
            @ApiParam(value = "时间类型",required=false) @RequestParam(value="time_value",required=false)String time_value,
            @ApiParam(value = "字符类型",required=false) @RequestParam(value="string_value",required=false)String string_value,
            @ApiParam(value = "备注",required=false) @RequestParam(value="remark",required=false)String remark
    ) throws ParseException {
        AppResult<sys_params> app = new AppResult<sys_params>();
        
        /*SQL.SelectSQL sql = SQL.select("name").from("qr_sys_params")
        		.where("name=#{name}")
        		.addParameter("name", name.trim());
  	  	List<Map<String,Object>> num = dbHelper.select(sql);
	    if(num.size()>0){
	    	app.no("");
	        app.setMsg("分类标识重复，添加失败");
	        return app;
	    }*/
        
        sys_params sysParams=new sys_params();
        if(!StringUtils.isEmpty(pid)){
            sysParams.setPid(Integer.parseInt(pid));
        }else{
       	 sysParams.setPid(0);
       }
        if(!StringUtils.isEmpty(class_name)){
            sysParams.setClass_name(class_name);
        }
        if(!StringUtils.isEmpty(name)){
            sysParams.setName(name);
        }
        if(!StringUtils.isEmpty(number_value)){
            sysParams.setNumber_value(Double.valueOf(number_value));
        }
        if(!StringUtils.isEmpty(date_value)){ 
            sysParams.setDate_value(new DateTimeUtils(date_value).toDate()); 
        }
        if(!StringUtils.isEmpty(time_value)){ 
            sysParams.setTime_value(new DateTimeUtils(time_value).toDate());
        }
        if(!StringUtils.isEmpty(string_value)){
            sysParams.setString_value(string_value);
        }
        if(!StringUtils.isEmpty(remark)){
            sysParams.setRemark(remark);
        }
        Integer i =  sysParamsService.addSysParams(sysParams);
        if(i>0){
            app.ok();
            app.setMsg("添加成功");
        }else{
            app.no("");
            app.setMsg("添加失败");
        }
        return app;
    }

    /**
     * 更改系统配置
     */
    @PostMapping(value = "/updateSysParams")
    @ApiOperation(value = "更改系统配置", httpMethod = "POST")
    public AppResult<sys_params> updateSysParams(
    	   @ApiParam(value = "父级id",required=false) @RequestParam(value="pid",required=false)String pid,
           @ApiParam(value = "分类名称",required=false) @RequestParam(value="class_name",required=false)String class_name,
           @ApiParam(value = "分类标识",required=false) @RequestParam(value="name",required=false)String name,
           @ApiParam(value = "数值类型",required=false) @RequestParam(value="number_value",required=false)String number_value,
           @ApiParam(value = "日期类型",required=false) @RequestParam(value="date_value",required=false)String date_value,
           @ApiParam(value = "时间类型",required=false) @RequestParam(value="time_value",required=false)String time_value,
           @ApiParam(value = "字符类型",required=false) @RequestParam(value="string_value",required=false)String string_value,
           @ApiParam(value = "备注",required=false) @RequestParam(value="remark",required=false)String remark,
           @ApiParam(value = "id",required=true) @RequestParam(value="id",required=false)String id
    ) throws ParseException {
        AppResult<sys_params> app = new AppResult<sys_params>();
        /*SQL.SelectSQL sql = SQL.select("name").from("qr_sys_params")
        		.where("name=#{name} and id!=#{id}")
        		.addParameter("name", name.trim())
        		.addParameter("id", id);
        List<Map<String,Object>> num = dbHelper.select(sql);
	    if(num.size()>0){
	    	app.NO("");
	        app.setMsg("分类标识重复，更新失败");
	        return app;
	    }*/
        sys_params sysParams=new sys_params();
        if(!StringUtils.isEmpty(pid)){
            sysParams.setPid(Integer.parseInt(pid));
        }else{
       	 sysParams.setPid(0);
       }
        if(!StringUtils.isEmpty(class_name)){
            sysParams.setClass_name(class_name);
        }
        if(!StringUtils.isEmpty(name)){
            sysParams.setName(name); 
        }
        if(!StringUtils.isEmpty(number_value)){
            sysParams.setNumber_value(Double.valueOf(number_value));
        }
        if(!StringUtils.isEmpty(date_value)){ 
            sysParams.setDate_value(new DateTimeUtils(date_value).toDate()); 
        }
        if(!StringUtils.isEmpty(time_value)){ 
            sysParams.setTime_value(new DateTimeUtils(time_value).toDate());
        }
        if(!StringUtils.isEmpty(string_value)){
            sysParams.setString_value(string_value);
        }
        if(!StringUtils.isEmpty(remark)){
            sysParams.setRemark(remark);
        }
        
        sysParams.setId(Integer.parseInt(id));
        Integer i =  sysParamsService.updateSysParams(sysParams);
        if(i>0){
            app.ok();
            app.setMsg("更新成功");
        }else{
            app.no("");
            app.setMsg("更新失败");
        }
        return app;
    }
    /**
     * 删除用户
     */
    @PostMapping(value = "/deleteSysParams")
    @ApiOperation(value = "删除系统配置", httpMethod = "POST")
    public AppResult<sys_params> deleteSysParams(@ApiParam(value = "主键id",required=true) @RequestParam(value="id",required=true)String id
    ){
        AppResult<sys_params> app = new AppResult<sys_params>();
        
        SQL.SelectSQL sql = SQL.select("*").from("qr_sys_params")
				.where("pid=#{id}").addParameter("id", id);
        List<Map<String,Object>> sysCategoryInfos = dbHelper.select(sql);
        if(sysCategoryInfos.size()>0){
        	app.no("");
			app.setMsg("删除失败,存在子参数");
			return app;
        }
        
        sys_params sysParams=new sys_params();
        sysParams.setId(Integer.parseInt(id));
        Integer i =  sysParamsService.deleteSysParams(sysParams);
        if(i>0){
            app.ok();
            app.setMsg("删除成功");
        }else{
            app.no("");
            app.setMsg("删除失败");
        }
        return app;
    }
     
}
